class Snake {
    constructor() {
        this.size = 20; // 单元格大小
        this.body = [{ x: 200, y: 200 }]; // 初始位置
        this.direction = { x: this.size, y: 0 }; // 初始方向（向右）
        this.alive = true; // 是否存活
    }

    move() {
        if (!this.alive) return;

        let newHead = {
            x: this.body[0].x + this.direction.x,
            y: this.body[0].y + this.direction.y
        };

        // 碰撞检测（撞墙 or 撞自己）
        if (
            newHead.x < 0 || newHead.y < 0 ||
            newHead.x >= canvas.width || newHead.y >= canvas.height ||
            this.body.some(segment => segment.x === newHead.x && segment.y === newHead.y)
        ) {
            this.alive = false;
            return;
        }

        this.body.unshift(newHead); // 头部前进
        this.body.pop(); // 移除尾部，保持长度

        console.log("蛇的坐标:", this.body); // 调试：检查蛇的坐标
    }

    checkEat(food) {
        if (this.alive && this.body[0].x === food.x && this.body[0].y === food.y) {
            this.body.push({}); // 增加长度
            return true;
        }
        return false;
    }

    reset() {
        this.body = [{ x: 200, y: 200 }];
        this.direction = { x: this.size, y: 0 };
        this.alive = true;
    }

    draw(ctx) {
        // 画蛇头（😋）
        ctx.font = "20px Arial"; 
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        ctx.fillText("🚞", this.body[0].x + this.size / 2, this.body[0].y + this.size / 2);

	    // 画蛇尾（⚾）
        for (let i = 1; i < this.body.length; i++) {
            ctx.font = "20px Arial";
            ctx.textAlign = "center";
            ctx.textBaseline = "middle";
            ctx.fillText("🚃", this.body[i].x + this.size / 2, this.body[i].y + this.size / 2); // 尾部是⚾
        }
    }
}

class Food {
    constructor(size) {
        this.size = size;
        this.spawn();
    }

    spawn() {
        this.x = Math.floor(Math.random() * (canvas.width / this.size)) * this.size;
        this.y = Math.floor(Math.random() * (canvas.height / this.size)) * this.size;
    }

    draw(ctx) {
        ctx.font = "20px Arial"; // 设置字体大小
        ctx.textAlign = "center"; // 文字居中对齐
        ctx.textBaseline = "middle"; // 文字基准点居中
        ctx.fillText("🍎", this.x + this.size / 2, this.y + this.size / 2);
    }
}

// 初始化
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const snake = new Snake();
const food = new Food(20);

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    snake.draw(ctx);
    food.draw(ctx);

    if (!snake.alive) {
        ctx.fillStyle = "white";
        ctx.font = "30px Arial";
        ctx.fillText("游戏结束", 130, 200);
        ctx.font = "20px Arial";
        ctx.fillText("按 Enter 重新开始", 120, 250);
    }
}

function update() {
    if (!snake.alive) return;
    snake.move();
    if (snake.checkEat(food)) food.spawn();
}

function gameLoop() {
    update();
    draw();
    setTimeout(gameLoop, 100); // 让蛇每100ms移动一次
}

// 监听键盘事件
document.addEventListener("keydown", (event) => {
    switch (event.key) {
        case "ArrowUp": if (snake.direction.y === 0) snake.direction = { x: 0, y: -snake.size }; break;
        case "ArrowDown": if (snake.direction.y === 0) snake.direction = { x: 0, y: snake.size }; break;
        case "ArrowLeft": if (snake.direction.x === 0) snake.direction = { x: -snake.size, y: 0 }; break;
        case "ArrowRight": if (snake.direction.x === 0) snake.direction = { x: snake.size, y: 0 }; break;
        case "Enter": snake.reset(); break;
    }
});

gameLoop();

